$:.unshift File.join(File.dirname(__FILE__),'..','lib')

require 'roadrunner'
# => I'll use the Httparty lib to make http request
# => as the LR method LoadRunner#web_url() and
# => LoadRunner#web_submit_data() and LoadRunner#web_custom_request()
require 'httparty'

# => if one of RoadRunner.new's argument is block
# => then the database connection will be established
# => and the below method could be used:
# => RoadRunner#save_report()

# => IMPORTANT:
# => if a block is given to RoadRunner#new
# => make sure there is exist the below three tables in the target DATABASE:
# => scenarios transactions records
# => and the three tables is defined in db.rb,
# => otherwise if the three tables does not exist in the target DATABASE,
# => Roadrunner will auto create the three tables.

getHawaii=RoadRunner.new
#do |session|
#  {:adapter=>"mysql",
#  :encoding=>"utf8",
#  :database=>"dpmp",
#  :username=>"root",
#  :password=>"alisoft",
#  :host=>"10.2.226.133"}
#end

# => RoadRunner#mode could be set as such value:
# => 'thread'
# => 'sequence' or other string is same.
# => when thread mode is set,
# => the RoadRunner#action will run as thread,
# => and main thread will fork RoadRunner#users threads to run every action block
#getHawaii.mode = 'thread'

getHawaii.init do
  # users决定同时有多少并发用户一起执行action
  # iterations决定每个用户执行多少次
  getHawaii.users,getHawaii.iterations=2,1
end

# => RoadRunner#any_other_method()
# => if any_other_method() is not exsit in RoadRunner#methods
# => the RoadRunner#any_other_method(){} will be dealed as RoadRunner#transaction('any_other_method'){}
# => and the important is:
# => the block of RoadRunner#transaction('any_other_method'){}
# => the value of the last expression in the block
# => will determin this transaction's current action is pass or not!!!
# =>
# => status's value is:
# => 0=>success
# => -1=>faile
# => ruby code as below:
# => case status
# => when false,'false','-1',-1 then status = -1
# => else status = 0
# => end
getHawaii.action do
  getHawaii.homepage do
    resp = HTTParty.get('http://www.hawaii.com/')
    # => check request header if contain the certain key words
    # => same as the LR method LoadRunner#web_reg_find("Search=Headers")
    resp.headers.inspect.include? 'Apache'
  end
  # => Log4r is integrade in the RoadRunner and you could use it as below.
  # => RoadRunner#log.debug
  # => RoadRunner#log.info
  # => RoadRunner#log.warn
  # => RoadRunner#log.error
  # => RoadRunner#log.fatal
  getHawaii.homepage2 do
    resp = HTTParty.get('http://www.hawaii.com/')
    # => check request body
    # => same as the LR method LoadRunner#web_reg_find()
    resp.body.include? 'CalendarPopup'
  end
  true
end

# => RoadRunner#run
# => this method will execute the RoadRunner script
# => the performance will start.
getHawaii.run
# => RoadRunner#report
# => this method will output the report to the STDOUT
getHawaii.report
# => RoadRunner#save_report
# => this method will save the report to database if the database connection is established
# => :script is one of the arguments
# => if the :script is set and the value of :script is a path,the file's content of the path will be saved in database.
getHawaii.save_report(:name=>"getHawaiiHomepage",:script=>__FILE__)